<template>
  <div>
    <div><h1>CG单据信息</h1></div>
    <div>
      <div>
        <el-form :inline="true" :model="formsap" :label-position="right" label-width="100px">
          <el-form-item label="sap单号:">
            <el-input v-model="formsap.sapNumber" class="saptext" type="text"></el-input>
            <el-button type="primary" @click="selectsap" icon="el-icon-searchsousuo">搜索</el-button>
          </el-form-item>
        </el-form>

      </div>
      <div>
        <h2>H表头</h2>
      </div>
      <div class="form-container">
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="采购凭证号:">
            <el-input class="jhtext" v-model="date.cg_h.EBELN" type="text"/>
          </el-form-item>
          <el-form-item label="供应商:">
            <el-input class="jhlxtext" v-model="date.cg_h.LIFNR" type="text"/>
          </el-form-item>
          <el-form-item label="供应商名称:">
            <el-input class="jhrqtext" v-model="date.cg_h.NAME1" type="text"/>
          </el-form-item>
          <el-form-item label="公司代码:">
            <el-input class="gystext" v-model="date.cg_h.BUKRS" type="text"/>
          </el-form-item>
          <el-form-item label="公司名称:">
            <el-input class="gysmctext" v-model="date.cg_h.BUTXT" type="text"/>
          </el-form-item>
          <el-form-item label="采购组织:">
            <el-input class="sjxmtext" v-model="date.cg_h.EKORG" type="text"/>
          </el-form-item>
          <el-form-item label="采购组织描述:">
            <el-input class="chtext" v-model="date.cg_h.EKOTX" type="text"/>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>I表头</h2></div>
      <div>
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="采购凭证编号">
            <el-input class="jhtext" v-model="date.cg_i.EBELN" type="Text"/>
          </el-form-item>
          <el-form-item label="采购凭证的项目编号">
            <el-input class="jhxmtext" v-model="date.cg_i.EBELP" type="Text"/>
          </el-form-item>
          <el-form-item label="物料编号">
            <el-input class="wlhtext" v-model="date.cg_i.MATNR" type="Text"/>
          </el-form-item>
          <el-form-item label="物料描述">
            <el-input class="wlmstext" v-model="date.cg_i.MAKTX" type="Text"/>
          </el-form-item>
          <el-form-item label="工厂">
            <el-input class="gctext" v-model="date.cg_i.WERKS" type="Text"/>
          </el-form-item>
          <el-form-item label="工厂名称">
            <el-input class="gcmctext" v-model="date.cg_i.NAME1" type="Text"/>
          </el-form-item>
          <el-form-item label="库存地点">
            <el-input class="kcddtext" v-model="date.cg_i.LGORT" type="Text"/>
          </el-form-item>
          <el-form-item label="库存地点的描述">
            <el-input class="kcmctext" v-model="date.cg_i.LGOBE" type="Text"/>
          </el-form-item>
          <el-form-item label="采购订单数量">
            <el-input class="jhsltext" v-model="date.cg_i.MENGE" type="Text"/>
          </el-form-item>
          <el-form-item label="采购订单计量单位">
            <el-input class="jbjldwtext" v-model="date.cg_i.MEINS" type="Text"/>
          </el-form-item>
          <el-form-item label="采购凭据中的净价">
            <el-input class="cgddtext" v-model="date.cg_i.NETPR" type="Text"/>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>备注信息</h2></div>
      <div>
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="承运商">

            <el-select class="saptext" v-model="selectedValue" placeholder="请选择">
              <el-option
                v-for="item in sapOptions"
                :key="item.id"
                :label="item.name"
                :value="item.LIFNR">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="车号">
            <el-input class="chehao" v-model="date.chehao" type="Text"/>
          </el-form-item>
          <el-form-item label="司机身份证">
            <el-input class="sjsfz" v-model="date.sjsfz" type="Text"/>
          </el-form-item>
          <el-form-item label="订单物料数量">
            <el-input class="ddwlsl" v-model="date.ddwlsl" type="Text"/>
          </el-form-item>
          <el-form-item label="原始单号">
            <el-input class="ysdh" v-model="date.ysdh" type="Text"/>
          </el-form-item>
          <el-form-item label="原始重量">
            <el-input class="yszl" v-model="date.yszl" type="Text"/>
          </el-form-item>
          <el-form-item label="原发日期">
            <el-input class="yfrq" v-model="date.yfrq" type="Text"/>
          </el-form-item>
          <el-form-item label="原发矿">
            <el-input class="yfk" v-model="date.yfk" type="Text"/>
          </el-form-item>
          <el-form-item label="规格">
            <el-input class="guige" v-model="date.guige" type="Text"/>
          </el-form-item>
          <el-form-item label="原始毛重">
            <el-input class="ysmz" v-model="date.ysmz" type="Text"/>
          </el-form-item>
          <el-form-item label="原始净重">
            <el-input class="ysjz" v-model="date.ysjz" type="Text"/>
          </el-form-item>
          <el-form-item label="玉米合同号">
            <el-select class="ymhttext" v-model="selectymhthValue" placeholder="请选择">
              <el-option
                v-for="item in ymhthOptions"
                :key="item.id"
                :label="item.hth"
                :value="item.hth">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="第三方">
            <el-input class="dsf" v-model="date.dsf" type="Text"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input class="openid" v-model="date.openid" type="Text"/>
          </el-form-item>

          <el-form-item label="超长车标记:">
            <el-checkbox
              v-model="date.yc_ccc"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>

          </el-form-item>
          <el-form-item label="内专车标记:">
            <el-checkbox
              v-model="date.NZBJ"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>

          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary"  v-hasPermi="['system:zd:CG_dj']" @click="submitData" icon="el-icon-searchsousuo">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>

import {
  counthmd,
  lisTb_chengys,
  lisTb_hetonghuayan,
  selectCgh, selectCgi,
  tbGuozhongExists,
  tbGuozhonginsert
} from "@/api/system/Fdongbei/zd";


export default {
  data() {
    return {
      right: "",
      formsap: {
        sapNumber: '' // 初始化表单数据
      },
      date: {
        cg_h: {
          EBELN: '',
          LIFNR: '',
          NAME1: '',
          BUKRS: '',
          BUTXT: '',
          EKORG: '',
          EKOTX: '',
        },
        cg_i: {
          EBELN: '',
          EBELP: '',
          MATNR: '',
          MAKTX: '',
          WERKS: '',
          NAME1: '',
          LGORT: '',
          LGOBE: '',
          MENGE: '',
          MEINS: '',
          NETPR: '',
        },
        chehao: '',
        sjsfz: '',
        ddwlsl: '',
        ysdh: '',
        yszl: '',
        yfrq: '',
        yfk: '',
        guige: '',
        ysmz: '',
        ysjz: '',
        dsf: '',
        NZBJ: ''
      },
      guozhong: {
        wlmcc: '',
        jsdj: '',
        card_id: '',
        chehao: '',
        czbh: '',
        wlbh: '',
        wlmc: '',
        gblx: '',
        gysbh: '',
        gysdm: '',
        ckbh: '',
        ckmc: '',
        dbh: '',
        gsbh: '',
        ddxmbh: '',
        gbzt: '',
        sjxm: '',
        sjdh: '',
        sjsfz: '',
        yzkz: '',
        ddwlsl: '',
        ddysdh: '',
        ddyszl: '',
        nlmtbz1: '',
        nlmtbz2: '',
        czymc: '',
        sapbzsc: '',
        yfrq: '',
        yfk: '',
        ddbh: '',
        ymqysj: '',
        guige: '',
        ddysmz: '',
        ddysjz: '',
        fk_zdr: '',
        kk_rjbb: '',
        ycCcc: '0',
        jshth: '',
        fksj: '',
        ysmz_bj: '',
        chexc: ''
      },
      selectedValue: '', // 这里需要初始化一个值,
      selectymhthValue: '',
      ymhthOptions: [],
      sapOptions: []
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      lisTb_chengys().then(response => {
        this.sapOptions = response.data
        // console.log(this.sapOptions)

      }),
        lisTb_hetonghuayan().then(response => {
          this.ymhthOptions = response.data
          console.log(this.ymhthOptions)

        })
    },
    selectsap() {


      try {
        selectCgh({ebeln: this.formsap.sapNumber}).then(response => {
          if (response.data === undefined) {
            this.$modal.alertWarning("Tb_cg_h缺失")
          }
          const data = response.data;
          this.date.cg_h = {
            EBELN: data.ebeln,
            LIFNR: data.lifnr,
            NAME1: data.name1,
            BUKRS: data.bukrs,
            BUTXT: data.butxt,
            EKORG: data.ekorg,
            EKOTX: data.ekotx,
          };
        });
      } catch (error) {
        console.error('请求失败:', error);
      }

      try {
        selectCgi({ebeln: this.formsap.sapNumber}).then(response => {
          if (response.data === undefined) {
            this.$modal.alertWarning("Tb_cg_i缺失")
          }
          const data = response.data;
          this.date.cg_i = {
            EBELN: data.ebeln,
            EBELP: data.ebelp,
            MATNR: data.matnr,
            MAKTX: data.maktx,
            WERKS: data.werks,
            NAME1: data.name1,
            LGORT: data.lgort,
            LGOBE: data.lgobe,
            MENGE: data.menge,
            MEINS: data.meins,
            NETPR: data.netpr,

          };

        });
      } catch (error) {
        console.error('请求失败:', error);
      }

    },
    submitData() {//提交
      if (this.date.chehao === "") {
        this.qk()
        this.$modal.alertWarning("车号不能为空")
        return
      }
      if (this.date.openid.length !== 11) {
        this.$modal.alertWarning("手机号位数不对")
        return
      }
      counthmd(this.date.chehao).then(response => {
        if (response.msg > 0) {
          this.$modal.alertWarning("黑名单用户")
        } else {

          tbGuozhongExists({chehao: this.date.chehao}).then(response => {
            if (response.msg === 'OK') {
              this.guozhong = {
                wlmcc: this.selectedValue,
                jsdj: this.date.cg_i.NETPR,
                card_id: '',
                chehao: this.date.chehao,
                czbh: 'DR',
                wlbh: this.date.cg_i.MATNR,
                wlmc: this.date.cg_i.MAKTX,
                gblx: 'CG',
                gysbh: this.date.cg_h.LIFNR,
                gysdm: this.date.cg_h.NAME1,
                ckbh: this.date.cg_i.LGORT,
                ckmc: this.date.cg_i.LGOBE,
                dbh: "",
                gsbh: this.date.cg_i.WERKS,
                ddxmbh: this.date.cg_i.EBELP,
                gbzt: '待进厂',
                sjxm: '',
                sjdh: '',
                sjsfz: this.date.sjsfz,
                yzkz: '0.00',
                ddwlsl: this.date.cg_i.MENGE,
                ddysdh: this.date.ysdh,
                ddyszl: this.date.yszl,
                nlmtbz1: '',
                nlmtbz2: '',
                czymc: this.$store.state.user.name,
                sapbzsc: '',
                yfrq: this.date.yfrq,
                yfk: this.date.yfk,
                ddbh: this.date.cg_h.EBELN,
                ymqysj: new Date().toISOString(),
                guige: this.date.guige,
                ddysmz: this.date.ysmz,
                ddysjz: this.date.ysjz,
                fk_zdr: this.$store.state.user.name,
                kk_rjbb: '',
                ycCcc: this.date.yc_ccc,
                jshth: this.selectymhthValue,
                fksj: new Date().toISOString(),
                ysmz_bj: '',
                chexc: '',
                lxdh:this.date.openid,
               // ymqysj:new Date().toISOString(),
              }
              //  console.log("开始访问后端")
              tbGuozhonginsert(this.guozhong).then(response => {
                if (response.msg != 0) {
                  this.qk()
                  this.$modal.alert("保存成功")

                } else {
                  this.qk()
                  this.$modal.alertWarning("保存失败")

                }
              })
            } else {
              this.qk()
              this.$modal.alertWarning(response.msg)
            }
          })


        }
      })

    },
    qk() {
      this.formsap = {
        sapNumber: '' // 初始化表单数据
      },

        this.date = {
          cg_h: {
            EBELN: '',
            LIFNR: '',
            NAME1: '',
            BUKRS: '',
            BUTXT: '',
            EKORG: '',
            EKOTX: '',
          },
          cg_i: {
            EBELN: '',
            EBELP: '',
            MATNR: '',
            MAKTX: '',
            WERKS: '',
            NAME1: '',
            LGORT: '',
            LGOBE: '',
            MENGE: '',
            MEINS: '',
            NETPR: '',
          },


          chehao: '',
          sjsfz: '',
          ddwlsl: '',
          ysdh: '',
          yszl: '',
          yfrq: '',
          yfk: '',
          guige: '',
          ysmz: '',
          ysjz: '',
          dsf: '',
          NZBJ: ''
        },
        this.guozhong = {
          wlmcc: '',
          jsdj: '',
          card_id: '',
          chehao: '',
          czbh: '',
          wlbh: '',
          wlmc: '',
          gblx: '',
          gysbh: '',
          gysdm: '',
          ckbh: '',
          ckmc: '',
          dbh: '',
          gsbh: '',
          ddxmbh: '',
          gbzt: '',
          sjxm: '',
          sjdh: '',
          sjsfz: '',
          yzkz: '',
          ddwlsl: '',
          ddysdh: '',
          ddyszl: '',
          nlmtbz1: '',
          nlmtbz2: '',
          czymc: '',
          sapbzsc: '',
          yfrq: '',
          yfk: '',
          ddbh: '',
          ymqysj: '',
          guige: '',
          ddysmz: '',
          ddysjz: '',
          fk_zdr: '',
          kk_rjbb: '',
          ycCcc: '0',
          jshth: '',
          fksj: '',
          ysmz_bj: '',
          chexc: ''
        },
        this.selectedValue = '', // 这里需要初始化一个值,
        this.selectymhthValue = ''

    }

  }
};

</script>

<style scoped lang="scss">
.el-icon-searchsousuo {
  display: flex;
  align-items: center;
}

.el-input {
  width: auto;
  flex-grow: 3;
}

.el-button {
  margin-left: 10px; /* 调整按钮与输入框之间的间距 */
  flex-shrink: 0; /* 防止按钮在宽度不足时被压缩 */
}

.input-with-select {
  background-color: #235982;
}
</style>
